﻿using MediatR;
using ProductionManagement.Application.Command.SendWorkersForQualityInspectionCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Infrstructre.lnterface;

namespace ProductionManagement.Application.CommandHandler.SendWorkersForQualityInspectionCommandHandlers
{
    public class SendWorkersForQualityInspectionSelectCommandHandlers : IRequestHandler<SendWorkersForQualityInspectionSelectCommand, SendWorkersForQualityInspectionSelectDtos>
    {
        protected readonly ISendWorkersForQualityInspectionRepository repository;

        public SendWorkersForQualityInspectionSelectCommandHandlers(ISendWorkersForQualityInspectionRepository repository)
        {
            this.repository = repository;
        }

        public async Task<SendWorkersForQualityInspectionSelectDtos> Handle(SendWorkersForQualityInspectionSelectCommand request, CancellationToken cancellationToken)
        {
            var size = (request.pageindex - 1) * request.pagesize;
            var workers = (await repository.GetAll()).Where(x => x.IsDel == false).ToList();
            if (request.PName != null)
            {
                workers = workers.Where(x => x.PName.Contains(request.PName)).ToList();
            }
            if (request.SWFIPeople != null)
            {
                workers = workers.Where(x => x.SWFIPeople == request.SWFIPeople).ToList();
            }
            SendWorkersForQualityInspectionSelectDtos page = new SendWorkersForQualityInspectionSelectDtos();
            page.pagecount = workers.Count();
            page.totalcount = (int)Math.Ceiling(workers.Count() * 1.0 / request.pagesize);
            page.list = workers.Skip(size).Take(request.pagesize).ToList();
            return page;
        }
    }
}
